home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / kjdc9308.zip / kdfilt.c < prev    next >
C/C++ Source or Header  |  1993-08-26  |  3KB  |  133 lines

  1. /* K D F I L T - filter program for kanjidic
  2.  
  3. See the "printf" statements below for some simple operating instructions
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. long int lno=0;
  15. FILE *fi,*fo,*fopen();
  16. unsigned char cc,c,instr[512],outstr[512],codes[20];
  17.  
  18. int i,ip,istate,op,ic;
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char **argv;
  23. {
  24.     int argk;
  25.     register char **p;
  26.     argk = argc;
  27.     if (argk < 3)
  28.     {
  29.         printf("usage: kdfilt fni -CODES fno\n\n ");
  30.         printf("KDFILT removes unwanted index codes from kanjidic\n\n ");
  31.         printf("  fni - the input file name (e.g. kanjidic)\n ");
  32.         printf("  fno - the output file containing the filtered kanjidic\n ");
  33.         printf("  -CODES the index codes you want removed, e.g. -MQY will \n        result in the Morohashi, Four-Corner & PinYin codes being removed.\n\n ");
  34.         exit(0);
  35.     }
  36.     p=argv;
  37.     p++;
  38.     fi= fopen(*p,"r");
  39.     if (fi == NULL)
  40.     {
  41.         printf("\nCannot open %s\n",*p);
  42.         exit(0);
  43.     }
  44.     p++;
  45.     strcpy(codes,*p);
  46.     if((codes[0] != '-') || (strlen(codes) < 2))
  47.     {
  48.         printf("usage: kdfilt fni -CODES fno\n ");
  49.         exit(0);
  50.     }
  51.     p++;
  52.     fo = fopen(*p,"w");
  53.     if(fo == NULL)
  54.     {
  55.         printf("open of %s failed\n",*p);
  56.         exit(0);
  57.     }
  58.     while (feof(fi) != TRUE)
  59.     {
  60.         fgets (instr,511,fi);
  61.         if(feof(fi)) break;
  62.         if(instr[0] == '#')
  63.         {
  64.             fprintf(fo,"%s",instr);
  65.             continue;
  66.         }
  67.         if((++lno % 10) == 0)printf("Line: %ld\r",lno);
  68.         strcat (instr,"\n");
  69.         for(ip = 0;ip <8;ip++)
  70.         {
  71.             outstr[ip] = instr[ip];
  72.         }
  73.         op = 8;
  74.         ip = 8;
  75.         istate = 0;
  76.         while (TRUE)
  77.         {
  78.             c = instr[ip];
  79.             switch (istate) {
  80.  
  81.             case 0 :    /* start of field    */
  82.                 if (c == ' ') break;
  83.                 if ((c == 0xa4) || (c == 0xa5) || (c == '{'))
  84.                 {
  85.                     for ( ; instr[ip] >= 0x20; ip++) outstr[op++] = instr[ip];
  86.                     c = 0x10;
  87.                     break;
  88.                 }
  89.                 if ( c < 0x20) break;
  90.                 ic = c;
  91.                 if( strchr(codes,ic) != NULL)
  92.                 {
  93.                     istate = 1;
  94.                     break;
  95.                 }
  96.                 outstr[op++] = c;
  97.                 istate = 2;
  98.                 break;
  99.  
  100.             case 1 :    /* in a field being skipped    */
  101.                 if ( c < 0x20) break;
  102.                 if ( c == ' ')
  103.                 {
  104.                     if(outstr[op-1] != ' ') outstr[op++] = ' ';
  105.                     istate = 0;
  106.                     break;
  107.                 }
  108.                 break;
  109.             case 2 :    /* in a field being copied */
  110.                 if ( c < 0x20) break;
  111.                 if ( c == ' ')
  112.                 {
  113.                     if(outstr[op-1] != ' ') outstr[op++] = ' ';
  114.                     istate = 0;
  115.                     break;
  116.                 }
  117.                 outstr[op++] = c;
  118.                 break;
  119.             }
  120.             if ( c < 0x20)
  121.             {
  122.                 if(outstr[op-1] != ' ') outstr[op++] = ' ';
  123.                 outstr[op] = 0;
  124.                 fprintf(fo,"%s\n",outstr);
  125.                 break;
  126.             }
  127.             ip++;
  128.         }
  129.     }
  130.     fclose(fi);
  131.     fclose(fo);
  132. }
  133.